<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\auth\Auth;
use app\common\model\Authgroup;
use app\common\model\Authgroupaccess;
use app\common\model\Users as UsersModel;

class Users extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $userDatas = UsersModel::paginate(5);
        return view('', compact('userDatas'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $authgroups = Authgroup::all();
        return view('', compact('authgroups'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = input();
        $result = $this->validate($data, 'Users');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        if (empty($data['group_id'])) {
            $this->error('请选择用户组');
        }
        //添加用户数据
        $user = new UsersModel;
        $user->username = $data['username'];
        $user->password = md5($data['password']);
        $user->save();
        //获取自增ID
        $userid = $user->id;
        //添加用户组
        $groupDatas = $data['group_id'];
        foreach ($groupDatas as $groupData) {
            $list[] = [
                'uid' => $userid,
                'group_id' => $groupData
            ];
        }

        $user = new Authgroupaccess();
        if ($user->saveAll($list)) {
            $this->success('添加成功', '/admin/user');
        } else {
            $this->error('操作有误');
        };
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //查询用户信息
        $userData = UsersModel::find($id);
        //查询该用户所属的用户组
        $ownGroups = Authgroupaccess::where('uid', $id)->column('group_id');
        //查询用户组
        $authgroups = Authgroup::all();
        return view('', compact('authgroups', 'userData', 'ownGroups'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input();
        $result = $this->validate($data, 'Users.edit');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        if ($data['password'] != $data['password2']) {
            $this->error('两次密码不一致');
        }
        if (empty($data['group_id'])) {
            $this->error('请选择用户组');
        }

        //添加用户数据
        $userData['username'] = $data['username'];
        if (!empty($data['password'])) {
            $userData['password'] = md5($data['password']);
        }
        UsersModel::where('id', $id)->update($userData);

        //修改用户组 先删除 再添加
        Authgroupaccess::where('uid', $id)->delete();
        $groupDatas = $data['group_id'];
        foreach ($groupDatas as $groupData) {
            $list[] = [
                'uid' => $id,
                'group_id' => $groupData
            ];
        }
        $user = new Authgroupaccess();
        if ($user->saveAll($list)) {
            $this->success('添加成功', '/admin/user');
        } else {
            $this->error('操作有误');
        };
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id == 1) {
            $res['status'] = '0';
            $res['msg'] = '禁止删除admin';
            return $res;
        }
        //删除用户组
        Authgroupaccess::where('uid', $id)->delete();
        //删除数据
        if (UsersModel::where('id', $id)->delete()) {
            $res['status'] = '1';
            $res['msg'] = '删除成功';
        } else {
            $res['status'] = '0';
            $res['msg'] = '删除失败';
        }
        return $res;
    }
}
